<template>
  <div class="wrapper white-card">
    <div class="search-box" ref="searchBar">
      <el-form ref="form" size="medium" label-width="80px" class="demo-form">
        <el-row>
          <div class="title">刷新顾问当天绩效分</div>
          <el-col :xs="12" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="顾问ID" prop="consultantId">
              <el-input
                placeholder="请输入顾问"
                v-model.trim="consultantId1"
                clearable
                @keyup.enter.native="handleRefreshPerformance"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item>
              <el-button type="primary" @click="handleRefreshPerformance"
                >刷新</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider></el-divider>
        <el-row>
          <div class="title">统计每日聊天回复时间</div>
          <el-col :xs="12" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="顾问ID" prop="consultantId">
              <el-input
                placeholder="请输入顾问,不传则计算全部顾问"
                v-model.trim="consultantId2"
                clearable
                @keyup.enter.native="handleReply"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="统计时间" prop="dateRange">
              <el-date-picker
                style="width: 100%"
                v-model="dateRange"
                type="daterange"
                range-separator="-"
                start-placeholder="开始月份"
                end-placeholder="结束月份"
                value-format="yyyy-MM-dd"
                clearable
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label-width="20px">
              <el-button type="primary" @click="handleReply">统计</el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider></el-divider>
        <el-row>
          <div class="title">计算月绩效分</div>
          <el-col :xs="12" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="顾问ID" prop="consultantId">
              <el-input
                placeholder="请输入顾问,不传则计算全部顾问"
                v-model.trim="consultantId3"
                clearable
                @keyup.enter.native="handleMonth"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label="统计月份" prop="startTime">
              <el-date-picker
                placeholder="请选择统计月份"
                style="width: 100%"
                v-model="startTime"
                type="month"
                value-format="yyyy-MM-dd"
                clearable
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="12" :md="8" :lg="6" :xl="6">
            <el-form-item label-width="20px">
              <el-button type="primary" @click="handleMonth">统计</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import { refresh, syncReply, syncPerformance } from "@/api";
export default {
  data() {
    return {
      consultantId1: "",
      consultantId2: "",
      consultantId3: "",
      dateRange: [],
      startTime: "",
    };
  },
  methods: {
    handleRefreshPerformance() {
      const params = {
        consultantId: this.consultantId1,
      };
      refresh(params).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: "刷新成功",
            type: "success",
          });
        }
      });
    },
    handleReply() {
      if (!Array.isArray(this.dateRange) || this.dateRange.length == 0) {
        this.$message.error("请先选择日期！");
      }
      let params = {
        consultantId: this.consultantId2,
      };
      params.startTime =
        Array.isArray(this.dateRange) && this.dateRange.length
          ? this.dateRange[0] + " 00:00:00"
          : "";
      params.endTime =
        Array.isArray(this.dateRange) && this.dateRange.length
          ? this.dateRange[1] + " 23:59:59"
          : "";
      syncReply(params).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: "统计成功",
            type: "success",
          });
        }
      });
    },
    getLastDay(year, month) {
      const date = new Date(year, month, 0);
      return date.getDate();
    },
    handleMonth() {
      let params = {
        consultantId: this.consultantId3,
        startTime: this.startTime + " 00:00:00",
      };
      syncPerformance(params).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: "计算月绩效分成功",
            type: "success",
          });
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.wrapper {
  height: 90vh;
  padding: 24px;
  margin: 24px;
  background-color: #fff;
  .title {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 24px;
  }
}
</style>
